package util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
//生成之后需要：
//1.实现实体类
//2.spring:	<import resource="beans/user.xml"/>
//3.hibernate:		<mapping resource="conf/hibernate/hbm/User.hbm.xml" />

public class GenerateSource {
	private static FileReader fileReader;
	private static BufferedReader bufferedReader;
	private static FileWriter fileWriter;
	private static Scanner scanner = new Scanner(System.in);

	//private final static String PROJECT_PACKAGE = "cn.edu.tsinghua.training.jeecms";
	//private final static String PROJECT_PACKAGE_PATH = "cn/edu/tsinghua/training/jeecms";
	
	//private final static String PATH_DAO = "java/" + PROJECT_PACKAGE_PATH + "/dao/model/";
	//private final static String PATH_DAO_IMPL = "java/" + PROJECT_PACKAGE_PATH + "/dao/impl/model/";
	//private final static String PATH_SERVICE = "java/" + PROJECT_PACKAGE_PATH + "/service/model/";
	//private final static String PATH_SERVICE_IMPL = "java/" + PROJECT_PACKAGE_PATH + "/service/impl/model/";
	//private final static String PATH_ENTITY = "java/" + PROJECT_PACKAGE_PATH + "/entity/";
	//private final static String PATH_ACTION = "java/" + PROJECT_PACKAGE_PATH + "/action/model/";
	//private final static String PATH_STRUTS = "resources/conf/struts/files/";
	//private final static String PATH_HBM = "resources/conf/hibernate/hbm/";
	//private final static String PATH_SPRING = "resources/conf/spring/beans/";
	
	//private final static String PROJECT_PACKAGE = "cn.edu.tsinghua.training.jeecms";
	//private final static String PROJECT_PACKAGE_PATH = "cn/edu/tsinghua/training/jeecms";
	
	private final static String PATH_DAO = "java/dao/model/";
	private final static String PATH_DAO_IMPL = "java/dao/impl/model/";
	private final static String PATH_SERVICE = "java/service/model/";
	private final static String PATH_SERVICE_IMPL = "java/service/impl/model/";
	private final static String PATH_ENTITY = "java/entity/";
	private final static String PATH_ACTION = "java/action/model/";
	private final static String PATH_STRUTS = "resources/conf/struts/files/";
	private final static String PATH_HBM = "resources/conf/hibernate/hbm/";
	private final static String PATH_SPRING = "resources/conf/spring/beans/";

	private static void generate(String template, String path, String x, String y) throws IOException {
		fileReader = new FileReader("resources/template/sourcecode/" + template + ".txt");
		bufferedReader = new BufferedReader(fileReader);
		StringBuffer stringBuffer = new StringBuffer();
		String string;
		while ((string = bufferedReader.readLine()) != null) {
			string = string.replaceAll("XXX", x);
			string = string.replaceAll("YYY", y);
			//string = string.replaceAll("demo", PROJECT_PACKAGE);
			stringBuffer.append(string + "\n");
		}
		bufferedReader.close();
		fileReader.close();
		
		String filename;
		if(template.equals("Entity")) {
			filename = x + ".java";
		} else if(template.equals("struts")) {
			filename = x + ".xml";
		} else if(template.equals("hbm")) {
			filename = x + ".hbm.xml";
		} else if(template.equals("spring")) {
			filename = y + ".xml";
		} else {
			filename = x + template + ".java";
		}
		fileWriter = new FileWriter(path + filename);
		fileWriter.write(stringBuffer.toString());
		System.out.println("\t" + filename + " generated!");
		fileWriter.flush();
		fileWriter.close();
	}

	public static void main(String[] args) throws IOException {
		System.out.println("enter entity class name, e.g:User");
		String entity = scanner.nextLine();
		System.out.println("enter primary key type, e.g:Long");
		String id = scanner.nextLine();

		generate("Dao", PATH_DAO, entity, id);//Dao
		generate("DaoImpl", PATH_DAO_IMPL, entity, id);//DaoImpl
		generate("Service", PATH_SERVICE, entity, id);//Service
		generate("ServiceImpl", PATH_SERVICE_IMPL, entity, id);//ServiceImpl
		generate("Entity", PATH_ENTITY, entity, "");//Entity
		generate("struts", PATH_STRUTS, entity.toLowerCase(), "");//Struts
		generate("Action", PATH_ACTION, entity, entity.toLowerCase());//Action
		generate("hbm", PATH_HBM, entity, "");//hbm
		generate("spring", PATH_SPRING, entity, entity.toLowerCase());//Spring

		System.out.println("refresh java and resources folder.");
	}
}
